<template>
  <div class="index-container">
    <div class="home-content">
      <div class="top-nav">
        <div class="page-content-wrap">
          <div class="phone" v-if="phone">
            联系电话：{{ phone }}
          </div>
          <div class="logo-wrap">
            <img src="../../assets/image/logo.png" alt="">
          </div>

          <div class="menu-list-wrap">
            <div class="item" @click="onClickNav('')">
              首页
            </div>
            <div class="split"></div>
            <div class="item" @click="onClickNav('rooms')">
              所有房间
            </div>
            <div class="split"></div>
            <div class="item" @click="onClickNav('admin')">
              后台管理
            </div>

            <div class="user-info">
              <div class="item" @click="onClickNav('login')" v-if="!login">
                登录
              </div>
              <div class="item" @click="onClickNav('sign')" v-if="!login">
                注册
              </div>
              <el-dropdown>
                <div class="item" v-if="login">
                  你好，{{ userinfo.name }}

                </div>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="onClickMy">我的预约</el-dropdown-item>
                    <el-dropdown-item @click="changePassword">修改密码</el-dropdown-item>
                    <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>


          </div>

          <div class="reserve-wrap">
            <div class="reserve-btn" @click="onClickNav('rooms')">
              现在预定
            </div>
          </div>

        </div>

      </div>

      <router-view/>

      <div class="footer-wrap">
        <div class="page-content-wrap" style="border-top: 1px solid #003a56;">
          <div class="link-wrap">
            <div class="item">住宿条款</div>
            <div class="split"></div>
            <div class="item">私人信息保护方针</div>
            <div class="split"></div>
            <div class="item">使用条款</div>
            <div class="split"></div>
            <div class="item">酒店&度假村</div>
          </div>
          <span class="right">Copyright © 2023 HAHA Miyako Hotels International, Inc. All rights reserved.</span>
        </div>
      </div>
    </div>

  </div>
</template>

<script setup lang="ts">
import {getCurrentInstance, onMounted, reactive, ref} from 'vue'
import {useRouter} from "vue-router";
import $api from "@/api";

const instance = getCurrentInstance()
const bus = instance.appContext.config.globalProperties.$bus
const router = useRouter()

onMounted(() => {
  refreshStatus()
  getConfig()
  bus.on('refresh-user-login-status', () => {
    refreshStatus()
  })
})
/**
 * 点击路由
 */
const onClickNav = (link: string) => {
  if (link === 'admin') {
    router.push('/admin')
  } else if (link === 'sign') {
    router.push('/sign')
  } else if (link === '') {
    router.push('/')
  } else if (link === 'login') {
    router.push('/login')
  } else if (link === 'rooms') {
    router.push('/rooms')
  }
}
/**
 * 检查登录状态
 */
const login = ref(false)
const userinfo = reactive({
  name: ''
})
const refreshStatus = () => {
  var rule = localStorage.getItem('rule')
  if (rule === 'USER' || rule === 'ADMIN' || rule === 'FRONT_USER') {
    login.value = true
    var _userinfo = localStorage.getItem('userinfo')
    _userinfo = JSON.parse(_userinfo)
    userinfo.name = _userinfo.name
  } else {
    login.value = false
  }

}
/**
 * 退出登录
 */
const logout = () => {
  $api.userApi.logout().then(res => {
    localStorage.clear()
    router.push('/')
    refreshStatus()
  })
}
/**
 * 查看我的预约
 */
const onClickMy = () => {
  router.push('/user')
}
/**
 * 修改密码
 */
const changePassword = () => {
  router.push('/password')
}
/**
 * 获取配置
 */
const phone = ref('')
const getConfig = () => {
  $api.configApi.fetchConfig().then(res => {
    phone.value = res.phone
  })
}
</script>
<style>
.page-content-wrap {
  width: 1200px;
  margin: 0 auto;
}

/*子模块标题*/
.user-title {
  padding: 20px 0;
  color: #3a3a3a;
  font-size: 14px;
  font-weight: bold;
}
</style>
<style scoped lang="scss">
.index-container {
  width: 100vw;
  min-height: 100vh;
  overflow-y: auto;
}


.home-content {
  width: 100%;
  padding-top: 110px;

  > .top-nav {
    width: 100%;
    background-color: #fff;
    height: 110px;
    position: fixed;
    top: 0;
    z-index: 99;
    padding-top: 10px;
    box-sizing: border-box;
    box-shadow: rgba(0, 0, 0, 0.05) 0px 0px 0px 1px;

    > .page-content-wrap {
      display: flex;
      height: 100%;
      justify-content: space-between;
      position: relative;

      > .phone {
        position: absolute;
        right: 0;
        top: -10px;
        font-size: 14px;
        line-height: 26px;
      }

      > .logo-wrap {
        width: 100px;
        height: 100px;

        > img {
          width: 160px;
        }
      }


    }
  }
}

.menu-list-wrap {
  display: flex;
  align-items: center;
  justify-content: center;

  > .item {
    color: #3a3a3a;
    font-size: 14px;
    margin: 0 30px;
    cursor: pointer;
    line-height: 1.5;

    &:hover {
      border-bottom: 1px solid #3a3a3a;
    }
  }

  > .split {
    border-left: 1px solid #3a3a3a;
    width: 1px;
    height: 20px;
  }
}

.reserve-wrap {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;

  > .reserve-btn {
    color: #fff;
    background: #3a3a3a;
    transition: all .4s ease;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 164px;
    height: 54px;
    cursor: pointer;

    &:hover {
      opacity: .75;
    }
  }
}

.footer-wrap {
  margin-top: 20px;
  height: 120px;

  .right {
    display: block;
    width: 100%;
    text-align: center;
    color: #003a56;
    font-size: 12px;
  }

  .link-wrap {
    padding-top: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 16px;

    > .item {
      color: #003a56;
      font-size: 16px;
      margin: 0 24px;
    }

    > .split {
      width: 0px;
      height: 20px;
      border-left: 1px solid #003a56;
    }
  }
}

.user-info {
  display: flex;
  margin-left: 200px;

  .item {
    color: #3a3a3a;
    font-size: 14px;
    cursor: pointer;
    line-height: 1.5;
    margin-left: 10px;
    display: flex;
    align-items: center;

    &:hover {
      border-bottom: 1px solid #3a3a3a;
    }
  }


}
</style>